import sun.reflect.generics.tree.Tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—07—11
 * Time: 18:17
 */
public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    //二叉数的根节点
    public TreeNode root;

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    // 前序遍历
    void preOrder(TreeNode root) {
        if (root == null) return;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    // 中序遍历
    void inOrder(TreeNode root) {
        if (root == null) return;
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    // 后序遍历
    void postOrde(TreeNode root) {
        if (root == null) return;
        postOrde(root.left);
        postOrde(root.right);
        System.out.print(root.val + " ");
    }
    // 获取树中节点的个数
    //1.子问题思路
    int size(TreeNode root) {
        if(root == null) return 0;
        return size(root.left) + size(root.right) + 1;
    }
    //2.遍历递归思路
    public static int sizeNode;
    void size2(TreeNode root){
        if(root == null) return;
        sizeNode++;
        size2(root.left);
        size2(root.right);
    }

    // 获取叶子节点的个数
    //1.子问题
    int getLeafNodeCount(TreeNode root){
        if(root == null) return 0;

        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }
    //2.遍历递归
    public static int leafSize;
    void getLeafNodeCount2(TreeNode root){
        if(root == null) return ;
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }

    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null) return 0;
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }

    // 获取二叉树的高度
    int getHeight(TreeNode root) {
        if(root == null) return 0;
        int leftHight = getHeight(root.left);
        int rightHight = getHeight(root.right);
        return (leftHight > rightHight ?
                leftHight + 1 :
                rightHight + 1);
    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val) {
        if(root == null) return null;
        if(root.val == val) {
            return root;
        }
        TreeNode ret1 = find(root.left,val);
        if (ret1 != null) {
            return ret1;
        }
        TreeNode ret2 = find(root.right,val);
        if (ret2 != null) {
            return ret2;
        }
        return null;
    }
    //层序遍历
    void levelOrder(TreeNode root) {
        if (root == null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        //现将根节点放进队列中
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            //只要子树的左右节点不为null就给队列里面放
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }
    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        if (root == null) return true;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();

            if (cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while(!queue.isEmpty()) {
            TreeNode cur = queue.peek();
            if (cur != null) {
                return false;
            }else {
                queue.poll();
            }
        }
        return true;
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q != null || p != null && q == null) {
            return false;
        }
        if(p == null && q == null) {
            return true;
        }
        if(p.val != q.val) {
            return false;
        }
        // p!=null && q!=null 并且 p.val == q.val
        return isSameTree(p.left,q.left)&& isSameTree(p.right,q.right);
    }

    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        int leftTree  = maxDepth(root.left);
        int rightTree = maxDepth(root.right);
        if (leftTree >= 0 && rightTree >= 0 && Math.abs(leftTree - rightTree) <= 1) {
            return Math.max(leftTree,rightTree) + 1;
        }else {
            return -1;
        }
    }
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        return maxDepth(root) >= 0;
    }

}
